package util;

import entity.Elev;
import entity.Fag;
import entity.Holdfag;
import entity.Potentiale;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import javax.print.PrintService;
import javax.swing.JOptionPane;

public abstract class PrintUtils {

    public static final int PT_INVALID = 0;
    public static final int PT_ELEV_INFO = 1;
    public static final int PT_ELEV_HANDLEPLAN_EVALUERING = 2;

    public static void printElevInfo(Elev e) {
        Document doc = new ElevInfoDocument(e);

        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(doc);
        job.setJobName(doc.getJobName());
        boolean accept = job.printDialog();
        if (accept) {
            try {
                job.print();
            } catch (PrinterException ex) {
                JOptionPane.showMessageDialog(null, "Kunne ikke udskrive.\n" + ex.getLocalizedMessage(), "Udskrifts fejl", 0);
            }
        }
    }

    public static void printElevHandleplan(Elev e) {
        ArrayList<Holdfag> hf = new ArrayList();
        hf.addAll(e.getHold().getHoldfags());

        boolean accept = false;
        PrintService ps = null;

        for (int i = 0; i < hf.size(); i++) {
            Fag f = hf.get(i).getFag();
            int hpSize = f.getHandleplans().size();
            int evSize = f.getEvaluerings().size();
            ArrayList<Potentiale> pt = new ArrayList();
            pt.addAll(e.getPotentiales());
            boolean pots = false;
            for (int p = 0; p < pt.size(); p++) {
                if (pt.get(p).getTrinbeskrivelse().getTrinoverskrift().getFag().getId().intValue() == f.getId().intValue()) {
                    pots = true;
                }
            }
            if (hpSize > 0 || evSize > 0 || pots) {
                Document doc = new ElevHPEvalDocument(e, f);
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPrintable(doc);
                job.setJobName(doc.getJobName());
                if (i == 0) {
                    accept = job.printDialog();
                    ps = job.getPrintService();
                } else {
                    try {
                        job.setPrintService(ps);
                    } catch (PrinterException ex) {
                        JOptionPane.showMessageDialog(null, "Kunne ikke udskrive.\n" + ex.getLocalizedMessage(), "Udskrifts fejl", 0);
                    }
                }
                if (accept) {
                    try {
                        job.print();
                    } catch (PrinterException ex) {
                        JOptionPane.showMessageDialog(null, "Kunne ikke udskrive.\n" + ex.getLocalizedMessage(), "Udskrifts fejl", 0);
                    }
                }
            }
        }

    }
}
